/*
 * @lc app=leetcode.cn id=206 lang=cpp
 *
 * [206] 反转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution
{
 public:
  /*ListNode* reverseList(ListNode* head) {
    if(!head) return NULL;
    //ListNode* hummy(-1);
    //hummy->next=head;
    ListNode* b,c;
    for(auto a=head;a->next;)
    {
      b=a->next;
      c=b;

      b->next=a;
      a=b;
      b=c;
    }
    head->next=NULL;
    head=b;

    return head;
  }*/

  ListNode* reverseList(ListNode* head)
  {
    // 找到前驱结点
    ListNode* pre = nullptr;

    auto cur = Head;

    while (cur)
    {
      auto next = cur->next;
      // 指向前驱
      cur->next = pre;

      // 下一次循环，指针换位置
      pre = cur;
      cur = next;
    }

    return pre;
  }
};
// @lc code=end
